package com.ws.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ws.model.ExamResult;
import com.ws.model.People;
import com.ws.persistence.ExamResultMapper;
import com.ws.persistence.PeopleMapper;
import com.ws.util.RandomUtil;

@Service("examResultsService")
public class ExamResultService {
	
	//private static final Logger LOGGER = Logger.getLogger(ExamResultService.class);
	
	@Resource
	private ExamResultMapper examResultMapper;
	@Resource
	private PeopleMapper peopleMapper;
	
	@Transactional
	public boolean insertExamResult(ExamResult examResult){
		List<People> list = peopleMapper.getListByIdCard(examResult.getIdCard());
		if(list.isEmpty()){
			return false;
		}
		People p = list.get(0);
		String cyryId = p.getCyryID();
		
		examResult.setCyryId(cyryId);
		examResult.setId(RandomUtil.RandomID(cyryId));
		//得到最大顺序标识
		Integer order = examResultMapper.selectMaxOrder(cyryId);
		//插入顺序为最大顺序加1
		examResult.setOrder(order==null?0:order+1);
		
		String auditorId1 = examResultMapper.selectAuditorIdByAuditorName(examResult.getAuditor1());
		String auditorId2 = examResultMapper.selectAuditorIdByAuditorName(examResult.getAuditor2());
		examResult.setAuditor1(auditorId1);
		examResult.setAuditor2(auditorId2);
		
		//插入成绩
		examResultMapper.insertExamResult(examResult);
		
		//修改从业人员档案状态
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", cyryId);
		
		//初领状态
		String status1[] = "1,2,3,4,5,6,7".split(",");
		
		//增加状态
		String status2[] = "11,12,13,14,15,16,17".split(",");
		
		Integer status = null;
		if(Arrays.asList(status1).contains(p.getStatus())){
			//初领考试合格
			status = 6;
		}else if(Arrays.asList(status2).contains(p.getStatus())){
			//增加考试合格
			status = 16;
		}
		
		params.put("status", status);
		peopleMapper.updateByParams(params);
		return true;
	}
}
